<?php

	/**
	 * Elgg profile edit form
	 *
	 * @package ElggProfile
	 *
	 * @uses $vars['entity'] The user entity
	 * @uses $vars['profile'] Profile items from $CONFIG->profile, defined in profile/start.php for now
	 */
?>


<div class="contentWrapper">
<form action="<?php echo $vars['url']; ?>action/profile/edit" method="post">
<?php echo elgg_view('input/securitytoken') ?>
<?php

//set is producer to false
$is_producer = false;

	//var_export($vars['profile']);
	if (is_array($vars['config']->profile) && sizeof($vars['config']->profile) > 0)
		foreach($vars['config']->profile as $shortname => $valtype) {
			if ($metadata = get_metadata_byname($vars['entity']->guid, $shortname)) {

				if (is_array($metadata)) {
					$value = '';
					foreach($metadata as $md) {
						if (!empty($value))
                                                    $value .= ', ';
						$value .= $md->value;
						$access_id = $md->access_id;
					}
				} else {
					$value = $metadata->value;
					$access_id = $metadata->access_id;
				}
			} else {
				$value = '';
				$access_id = ACCESS_DEFAULT;
			}

?>

	<p>
		<label>
			<?php  
			if($shortname=="categories"){
				if($is_producer)
				echo elgg_echo("profile:{$shortname}");
			}
			else{
				echo elgg_echo("profile:{$shortname}");
			}
				 
			
			?><br />
			<?php 
						/*
                            if ($shortname == 'county'){
                            	
                            	//capitalize to match the value
                         		$value = ucfirst($value);
                               $body = elgg_view("profile/forms/counties", array('value' => $value,)) . '</br>';
                               echo $body;



                            }
      
                            else*/ 
                            if($shortname=="producer"){
                                      
                            	if(!empty($value)){
                            		$is_producer = true;
                            		echo "<input type='checkbox' checked='checked' class='input-checkboxes' name='$shortname' value='yes' /></label><br />";	                            		
                            	}
                            	else{
                            		echo "<input type='checkbox' class='input-checkboxes' name='$shortname' value='yes' /></label><br />";	                            		
                            	}
                            }
                            	//categories should only appear if user is a producer
		                     else if($shortname == 'categories'){
                            	if($is_producer){
			                            $options = array(	//'metadata_name_value_pairs'	=>	array('product_type_id' => $product_type_id),
										'types'				=>	"object",
										'subtypes'			=>	"category",						
										'limit'				=>	9999,
									
											);
											
										$category_lists = elgg_get_entities_from_metadata($options);
											
										$values = "";
										foreach($category_lists as $cat){
											$categories[$cat->title] = $cat->guid;
										}
										
										$x = 0;
										foreach($value as $val){
											$test[$x] = $val; 
										}
									
										$selected = explode(",", $value);
										
			
			                            
			                            echo elgg_view("input/checkboxes",array('internalname' => $shortname,
										'value' => $value, 'options' => $categories, 'value' => $selected)); 	
                            	}
                            } 	
                            	else
                            	{
                                 echo elgg_view("input/{$valtype}",array('internalname' => $shortname,
									 'value' => $value,)); 
                            	}
                           
                            	
                                 ?>
		</label>

	</p>

<?php

		}
				echo elgg_view('input/access',array('internalname' => 'accesslevel', 'value' => $access_id));
?>

	<p>
		<input type="hidden" name="username" value="<?php echo page_owner_entity()->username; ?>" />
		<input type="submit" class="submit_button" value="<?php echo elgg_echo("save"); ?>" />
	</p>

</form>
</div>